package com.yaoweigroup.community.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.yaoweigroup.community.R;
import com.yaoweigroup.community.config.Urls;
import com.yaoweigroup.community.enity.Discount;
import com.yaoweigroup.community.ui.bm.DetailActivity;
import com.yaoweigroup.community.ui.home.DiscountDetailActivity;
import com.yaoweigroup.community.util.BitmapHelp;
import com.yaoweigroup.community.util.DateUtil;

public class DiscountAdapter extends BaseAdapter {

	private Context context;
	private List<Discount> list;
	private LayoutInflater mInflater;
	private BitmapUtils bitmapUtils;
	private BitmapDisplayConfig bigPicDisplayConfig;

	public DiscountAdapter(Context context, List<Discount> list) {
		mInflater = LayoutInflater.from(context);
		this.context = context;
		this.list = list;

		bitmapUtils = BitmapHelp.getBitmapUtils(context);
		bigPicDisplayConfig = new BitmapDisplayConfig();
		bigPicDisplayConfig.setLoadFailedDrawable(context.getResources()
				.getDrawable(R.drawable.bucket_no_picture));
		bigPicDisplayConfig.setLoadingDrawable(context.getResources()
				.getDrawable(R.drawable.bucket_no_picture));
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder;
		if (convertView == null) {
			holder = new Holder();
			convertView = mInflater.inflate(R.layout.adapter_discount_item,
					null);
			convertView.setTag(holder);

			holder.buy_btn = (Button) convertView.findViewById(R.id.buy_btn);
			holder.icon_iv = (ImageView) convertView.findViewById(R.id.icon_iv);
			holder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
			holder.discount_tv = (TextView) convertView
					.findViewById(R.id.discount_tv);
			holder.discount_num_tv = (TextView) convertView
					.findViewById(R.id.discount_num_tv);
			holder.buyNum_tv = (TextView) convertView
					.findViewById(R.id.buyNum_tv);
			holder.start_time_tv = (TextView) convertView
					.findViewById(R.id.start_time_tv);
			holder.end_time_tv = (TextView) convertView
					.findViewById(R.id.end_time_tv);
			holder.price_tv = (TextView) convertView
					.findViewById(R.id.price_tv);
			holder.buy_tv = (TextView) convertView.findViewById(R.id.buy_tv);
			holder.look_btn = (Button) convertView.findViewById(R.id.look_btn);
			holder.tip_tv = (TextView) convertView.findViewById(R.id.tip_tv);
		} else {
			holder = (Holder) convertView.getTag();
		}

		final Discount dicount = list.get(position);
		bitmapUtils.display(holder.icon_iv, dicount.getImageurl(),
				bigPicDisplayConfig);
		holder.name_tv.setText(dicount.getTitle());
		holder.start_time_tv.setText("开始时间:" + dicount.getStarttime());
		holder.end_time_tv.setText("结束时间:" + dicount.getEndtime());
		holder.price_tv.setText("面值金额：" + dicount.getPrice());
		int day = DateUtil.compare_date(dicount.getEndtime());
		if (day > 0) {
			holder.buy_btn.setVisibility(View.GONE);
			holder.tip_tv.setVisibility(View.VISIBLE);
		} else {
			holder.buy_btn.setVisibility(View.VISIBLE);
			holder.tip_tv.setVisibility(View.GONE);
		}

		holder.buy_tv.setText("已有" + dicount.getBuycount() + "人购买    已有"
				+ dicount.getReadcount() + "人浏览");

		holder.look_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent intent = new Intent(context, DetailActivity.class);
				intent.putExtra("id", dicount.getSid() + "");
				context.startActivity(intent);
			}
		});
		holder.buy_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent intent = new Intent(context,
						DiscountDetailActivity.class);
				intent.putExtra("discount", dicount);
				context.startActivity(intent);
				// Toast.makeText(context, "正在开发中,请期待!", 500).show();
			}
		});
		return convertView;
	}

	public final class Holder {

		Button buy_btn;
		ImageView icon_iv;
		TextView name_tv;
		TextView discount_tv;
		TextView discount_num_tv;
		TextView buyNum_tv;
		TextView start_time_tv;
		TextView end_time_tv;
		TextView price_tv;
		TextView buy_tv;
		Button look_btn;
		TextView tip_tv;

	}

	public void setDataChange(List<Discount> list) {
		this.list = list;
		notifyDataSetChanged();
	}

}
